//
//  WHExpressSelectView.m
//  SquirrelSeller
//
//  Created by 邓伟浩 on 2019/1/6.
//  Copyright © 2019 邓伟浩. All rights reserved.
//

#import "WHExpressSelectView.h"
#import "WHTextTipsCell.h"

@interface WHExpressSelectView ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UIView *backContentView;
@property (nonatomic, strong) UIView *infoContentView;
@property (nonatomic, strong) UIButton *cancelButton;

@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UILabel *headerLabel;

@property (nonatomic, strong) UITableView *infoTableView;

@property (nonatomic, assign) NSInteger selectIndex;
@property (nonatomic, assign) double viewHeight;

@property (nonatomic, strong) NSArray *dataArray;
@property (nonatomic, copy) NSString *headerTitle;
@property (nonatomic, copy) NSString *bottomTitle;

@end

@implementation WHExpressSelectView

- (instancetype)initWithArray:(NSArray *)dataArray
                  headerTitle:(NSString *)headerTitle
                  bottomTitle:(NSString *)bottomTitle {
    if (self = [super initWithFrame:CGRectZero]) {
        _dataArray = dataArray;
        _headerTitle = headerTitle;
        _bottomTitle = bottomTitle;
        [self initParams];
        [self setUpUI];
        
    }
    return self;
}



- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initParams];
        [self setUpUI];
        
    }
    return self;
}

- (void)initParams {
    _selectIndex = -1;
    
    NSUInteger cellCount = _dataArray.count<6?_dataArray.count:6;
    _viewHeight = (cellCount+2)*45;
}

- (void)setUpUI {
    [self addSubview:({
        _backContentView = [[UIView alloc] init];
        _backContentView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
        UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGes)];
        [_backContentView addGestureRecognizer:tapGes];
        _backContentView;
    })];
    [_backContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self);
    }];
    
    [self addSubview:({
        _infoContentView = [[UIView alloc] init];
        _infoContentView.backgroundColor = KWhiteColor;
        _infoContentView;
    })];
    [_infoContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self);
        make.bottom.mas_equalTo(self.mas_bottom).offset(kRealwidth(_viewHeight));
        make.height.mas_equalTo(kRealwidth(_viewHeight));
    }];
    
    [_infoContentView addSubview:({
        _headerView = [[UIView alloc] init];
        ViewBorderRadius(_headerView, 0, 0.5, CLineColor);
        _headerView;
    })];
    [_headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(_infoContentView);
        make.height.mas_equalTo(kRealwidth(45));
    }];
    
    [_headerView addSubview:({
        _headerLabel = [[UILabel alloc] init];
        _headerLabel.font = SYSTEMFONT(kRealwidth(15));
        _headerLabel.textColor = HexColor(@"999999");
        _headerLabel.textAlignment = NSTextAlignmentCenter;
        _headerLabel.text = _headerTitle;
        _headerLabel;
    })];
    [_headerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(_headerView.mas_centerY);
        make.height.mas_equalTo(kRealwidth(15));
        make.centerX.mas_equalTo(_headerView.mas_centerX);
    }];
    
    [_infoContentView addSubview:({
        _cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cancelButton setTitleColor:KWhiteColor forState:UIControlStateNormal];
        _cancelButton.titleLabel.font = SYSTEMFONT(kRealwidth(15));
        _cancelButton.backgroundColor = CMDarkOrangeColor;
        [_cancelButton addTarget:self action:@selector(cancelSelect:) forControlEvents:UIControlEventTouchUpInside];
        [_cancelButton setTitle:_bottomTitle forState:UIControlStateNormal];
        ViewBorderRadius(_cancelButton, 0, 0.5, CLineColor);
        _cancelButton;
    })];
    [_cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(_infoContentView);
        make.height.mas_equalTo(kRealwidth(45));
    }];
    
    [_infoContentView addSubview:({
        _infoTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        [_infoTableView registerClass:[WHTextTipsCell class] forCellReuseIdentifier:NSStringFromClass([WHTextTipsCell class])];
        _infoTableView.tableFooterView = [UIView new];
        _infoTableView.separatorColor = CLineColor;
        _infoTableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
        _infoTableView.delegate = self;
        _infoTableView.dataSource = self;
        
        _infoTableView;
    })];
    [_infoTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_headerView.mas_bottom);
        make.left.right.mas_equalTo(_infoContentView);
        make.bottom.mas_equalTo(_cancelButton.mas_top);
    }];
    
    _backContentView.alpha = 0;
    [UIView animateWithDuration:.3f animations:^{
        [_infoContentView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(self);
            make.bottom.mas_equalTo(self.mas_bottom);
            make.height.mas_equalTo(kRealwidth(_viewHeight));
        }];
        [self layoutIfNeeded];
        _backContentView.alpha = 1;
    } completion:^(BOOL finished) {
        
    }];
}

#pragma mark - —————— UITableViewDataSource & UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WHTextTipsCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([WHTextTipsCell class]) forIndexPath:indexPath];
    cell.tipsLabel.text = self.dataArray[indexPath.row];
    if (_selectIndex == indexPath.row) {
        cell.tipsLabel.textColor = CMDarkOrangeColor;
    } else {
        cell.tipsLabel.textColor = CFontTitleBlackColor;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    _selectIndex = indexPath.row;
    [tableView reloadData];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kRealwidth(45);
}

#pragma mark - —————— 事件 ——————

- (void)selectDone {
    if (self.selectExpress) {
        self.selectExpress(_selectIndex);
        
    }
    [self dismiss];
}

- (void)cancelSelect:(UIButton *)button {
    if (_selectIndex != -1) {
        [self selectDone];
    }
}

- (void)tapGes {
    [self dismiss];
}

- (void)show {
    [kAppWindow addSubview:self];
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(kAppWindow);
        if (@available(iOS 11.0, *)) {
            make.bottom.mas_equalTo(kAppWindow.mas_safeAreaLayoutGuideBottom);
        } else {
            make.bottom.mas_equalTo(kAppWindow.mas_bottom);
        }
    }];
}

- (void)dismiss {
    [UIView animateWithDuration:.3f animations:^{
        [_infoContentView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(self);
            make.bottom.mas_equalTo(self.mas_bottom).offset(kRealwidth(_viewHeight));
            make.height.mas_equalTo(kRealwidth(_viewHeight));
        }];
        [self layoutIfNeeded];
        _backContentView.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)dealloc {
    DLog(@"WHExpressSelectView");
}

@end
